<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="Val"
      ></el-input>
      <el-button @click="cleanSearch">清除</el-button>
      <el-button type="primary" @click.enter="Search">搜索</el-button>
    </div>
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80" prop="id">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="upList(row.id)"
            >编辑</el-button
          >
          <!-- 删除 -->
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delList(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <!--分页 -->
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        @current-change="pageChange"
      />
      <el-button size="small" plain @click="openAdd">+ 新增</el-button>
    </div>
    <el-dialog title="弹框" :visible="dialogVisible" @close="close">
      <!-- 表单内容 -->
      <template #default>
        <!-- 进行表单验证，添加表单对象和规则对象，及双向绑定数据渲染 -->
        <el-form ref="addForm" :model="Form" :rules="rules" label-width="100px">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="Form.name" />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="Form.des" />
          </el-form-item>
        </el-form>
      </template>
      <!-- ------------底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="cancel">取消</el-button>
        <el-button size="small" type="primary" @click="confirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDataList,
  addDataList,
  ReturnList,
  updataDataList,
  SearchList,
  DeleteDataList,
} from "@/api/index.js";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0,
      Val: "",
      dialogVisible: false,
      Form: {
        name: "",
        des: "",
        id: "",
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        des: [{ required: true, message: "描述不能为空", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await getDataList(this.params);
      this.list = res.data;
      const total = Number(res.headers["x-total-count"]);
      this.total = total;
      // console.log(this.total);
      // console.log(this.list);
    },
    pageChange(page) {
      this.params._page = page;
      this.getList();
    },
    openAdd() {
      this.dialogVisible = true;
    },
    close() {
      this.dialogVisible = false;
      // this.$refs.Form.resetFields();
      this.Form = {
        name: "",
        des: "",
        id: "",
      };
    },
    cancel() {
      this.dialogVisible = false;
      // this.$refs.Form.resetFields();
      this.Form = {
        name: "",
        des: "",
        id: "",
      };
    },
    confirm() {
      // 兜底
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.Form.id) {
            await updataDataList(this.Form);
            this.$message.success("编辑成功");
          } else {
            this.Form.id = uuidv4();
            await addDataList(this.Form);
            this.$message.success("新增成功");
          }
          // 提示
          // 关闭弹框
          this.dialogVisible = false;
          this.getList();
          // 重置表单
          // this.$refs.Form.resetFields();
          // 清空数据
          this.Form = {
            name: "",
            des: "",
            id: "",
          };
        }
      });
    },
    async upList(id) {
      const res = await ReturnList(id);
      this.Form = res.data;
      this.dialogVisible = true;
    },
    async Search() {
      const res = await SearchList(this.Val);
      this.list = res.data;
    },
    async cleanSearch() {
      this.Val = "";
      this.getList();
    },
    async delList(id) {
      await DeleteDataList(id);
      this.getList();
      this.$message.success("删除成功");
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
